<?php
/**
 * 租用记录
 * @Author    紫梦
 * @DateTime  2018-01-09
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\Rents as RentModel;
use app\common\model\Cars;
use app\common\request\Oss;

class Rent extends Base
{
	public function index()
	{
		$request = Request::instance();
        $post = $request->post();
        
        if($request->isAjax()){
            $datemin = $post['datemin']??'';
            $datemax = $post['datemax']??'';
            $seach = $post['search']??'';
	        if(!empty($datemin) && !empty($datemax) && !empty($seach)){
	            $where = [
	                'car_sn|car_unique_id|car_id|rent|hire'=>['like','%'.$seach.'%'],
	                'rent_time'=>['between time',[$datemin,$datemax]]
	                ];
	        }elseif (empty($datemin) && !empty($datemax) && !empty($seach)){	            
                $where = [
                	'rent_time'=>['<= time',$datemax],
                	'car_sn|car_unique_id|car_id|rent|hire'=>['like','%'.$seach.'%']
                ];
	        }elseif (!empty($datemin) && empty($datemax) && !empty($seach)) {
	        	$where = [
	                'rent_time'=>['> time',$datemin],
                	'car_sn|car_unique_id|car_id|rent|hire'=>['like','%'.$seach.'%']
	                ];
	        }elseif (!empty($datemin) && !empty($datemax) && empty($seach)) {
	        	$where = [
	                'rent_time'=>['between time',[$datemin,$datemax]]
	                ];
	        }elseif(empty($datemin) && empty($datemax) && !empty($seach)){
	        	$where = [
	                'car_sn|car_unique_id|car_id|rent|hire'=>['like','%'.$seach.'%']
	                ];
	        }elseif(!empty($datemin) && empty($datemax) && empty($seach)){
	        	$where = [
	                'rent_time'=>['> time',$datemin]
	                ];
	        }elseif(empty($datemin) && !empty($datemax) && empty($seach)){
	        	$where = [
	                'rent_time'=>['<= time',$datemax]
	                ];
	        }else{
	        	$where = '1=1';
	        }
            $model= RentModel::where($where)
                            ->order('id', 'desc')
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);


            if(count($model)>=1){

                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                    $res['data']= '';
                    $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }
            
            return $res;

        }else{
            
            $model = RentModel::count();
            $this->assign('number',$model);

        // // dump($post);
            $this->assign('search_value',$post['search']??null);    
            $this->assign('datemin',$post['datemin']??null);    
            $this->assign('datemax',$post['datemax']??null);    
            return $this->fetch();
        }

	}

    /**
     * 添加租用记录
     */
    public function add()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            if($data){               
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            }
            if(empty($car)){
                $msg['status'] = 1;
                $msg['msg'] = '车牌号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_unique_id'] = $car['car_unique_id'];
            
            $model = new RentModel();                 
            $model->allowField(true)->save($data);

            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('rent_file')->insert(['rent_id'=>$model->id,'file_url'=>$url]);
                    }
                }

            }   
            if($model->id){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }
            return $msg;

        }else{
            return $this->fetch();

        }
    }

    /**
     * 编辑租用记录
     */
    public function edit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            
            if(empty($car)){
                $msg['status'] = 1;
                $msg['msg'] = '车牌号不存在,请查正后再修改';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_unique_id'] = $car['car_unique_id'];
            $model = new RentModel();
            
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }
           
            
            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('rent_file')->insert(['rent_id'=>$data['id'],'file_url'=>$url]);
                    }
                }

            } 

            $msg['msg'] = '更新成功';
            return $msg;
        }else{

            $model = RentModel::get($id);          
            $this->assign('data',$model);
            return $this->fetch();

        }
    }

    /**
     * 租用车辆取回
     */
    public function revert($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $data['status'] = 2;
            $model = new RentModel();
            
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }
            
            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('rent_file')->insert(['rent_id'=>$data['id'],'file_url'=>$url]);
                    }
                }

            } 

            $msg['msg'] = '更新成功';
            return $msg;
        }else{
            $model = RentModel::get($id);          
            $this->assign('data',$model);
            return $this->fetch();
        }
    }

    /**
     * 删除租用记录附件
     */
    public function del_file($id)
    {
        $msg['status'] = 0;
        $url = Db::name('rent_file')->where(['id'=>$id])->find();
        $oss = new Oss();
        $res = false;
        if($oss->unlinkOss($url['file_url'])){
            $res = Db::name('rent_file')->delete($id);
            
        }
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 删除租用记录
     */
    public function delete($id)
    {
        $msg['status'] = 0;
        $res = RentModel::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }	

}